package com.project.customview.annotation;

import android.app.Activity;
import android.util.Log;
import android.view.View;

import com.project.customview.R;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Inject {
    public static final String TAG = "Reflection";

    public static void inject(Object obj){
        try {
            injectView(obj);
            injectClick(obj);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    private static void injectView(Object obj) throws IllegalAccessException {
        Class clazz = obj.getClass();
        Log.i(TAG,clazz.getName());
        Field[] fields = clazz.getFields();
        for (Field field:fields){
            InjectView injectView = field.getAnnotation(InjectView.class);
            if (injectView!=null&&injectView.value()!=0){
                int id = injectView.value();
                field.setAccessible(true);
                if (obj instanceof Activity){
                    field.set(obj,((Activity) obj).findViewById(id));
                }
            }

        }

    }
    private static void injectClick(final Object obj){
        Class clazz = obj.getClass();
        Method[] methods = clazz.getDeclaredMethods();
        for (final Method method:methods){
            OnClick click = method.getAnnotation(OnClick.class);
            OnLongClick longClick = method.getAnnotation(OnLongClick.class);
            if (click!=null&&click.value()!=0){
                View view = null;
                if (obj instanceof  Activity)
                    view = ((Activity) obj).findViewById(click.value());

                if (view==null)
                    return;
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            method.invoke(obj,v);//通过反射来调用被注解修饰的方法，把View传回去
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
            if (longClick!=null&&longClick.value()!=0){
                View view = null;
                if (obj instanceof  Activity)
                    view = ((Activity) obj).findViewById(click.value());

                if (view==null)
                    return;
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            method.invoke(obj,v);//通过反射来调用被注解修饰的方法，把View传回去
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
    }
}
